﻿$(function(){
	//初始化图片数列
	setPicListNum();
	
	//ie添加滚动属性
	if($.browser.msie){
		$("html:first").css("overflow","scroll")
	}
	
	//按钮部分 鼠标经过事件
	$("#hyShowBtn li").hover(function(){
		$(this).addClass("hover");
	},function(){
		$(this).removeClass("hover");
	})
	//事件触发切换效果
	$("#hyShowBtn li").live("click",function(){
		var num = $(this).index();
		showPicNum(num);
	})
	//触发向右切换
	$("#btnGoRight").live("click",function(){
		var $picAry = $("#hyShowImg img");
		var num = $picAry.index($picAry.filter(".selected")) + 1;
		if(num == ($picAry.length)){num = 0};
		showPicNum(num);
	})
	//触发向左切换
	$("#btnGoLeft").live("click",function(){
		var $picAry = $("#hyShowImg img");
		var num = $picAry.index($picAry.filter(".selected")) - 1;
		if(num < 0){num = $picAry.length-1;}
		showPicNum(num);
	})
	//写入图片的宽度和高度
	$("#hyShowImg img").each(function(){
		this.onload
	})
	
	//ie6 全屏
	//goFullWindow(".goToBig:eq(0)");
	
	$("#hyShowImg img.selected").load($(this).attr("src"),function(){
		timeOutSize();
	})
	
	
})

//初始化图片排序和图片序号
function setPicListNum(){
	var $picAry = $("#hyShowImg").find("img");
	var $numBox = $("#hyShowBtn ul");
	var html = "";
	for(var i = 0,len = $picAry.length;i<len;i++){
		var j = i + 1;
		var selected = '';
		html += '<li>';
		if(j == 1){selected = 'selected';}//判断是否是第一个
		html += '<a class="' + selected + '" href="javascript:;" attr="'+j+'">' + j + '</a>';
		html += '</li>';
	}
	$numBox.html(html);
	timeOutSize();//重置宽高
}
//触发图片高亮
function showPicNum(num){
	var $picAry = $("#hyShowImg img");
	var $numAry = $("#hyShowBtn li");
	var oldNum = $picAry.index($picAry.filter(".selected"));
	if(num == oldNum){return false;}
	$picAry.filter(".selected").removeClass("selected").hide();
	$numAry.find(".selected").removeClass("selected");
	$picAry.eq(num).addClass("selected").show();
	$numAry.eq(num).find("a").addClass("selected");
	timeOutSize();//重置宽高
}
//计算图片的宽高
var hy;
function timeOutSize(){
	window.clearTimeout(hy);
	var img = $("#hyShowImg img.selected");
	hy = window.setTimeout(setImgHW,10,img);
}

function setImgHW(img){
	var imgObj = img;
	var winHeight = getTotalHeight();//获得窗口高
	var winWidth = getTotalWidth();//获得窗口宽
	var winRate = winWidth/winHeight;//窗口宽高比
	var imgHeight = GetImageHeight(imgObj[0]);//图片高
	var imgWidth = GetImageWidth(imgObj[0]);//图片宽
	var imgRate = imgWidth/imgHeight;//图片宽高比
	var autoTune = 17;
	if($.browser.msie){autoTune = 0;}
	if(winRate >= imgRate){
		imgObj.css({
			width:winWidth - autoTune,
			height:winWidth*(1/imgRate)
			//marginTop:(winHeight - winWidth*(1/imgRate))/2,
			//marginLeft:0
		});
		return false;
	}
	if(winRate < imgRate){
		imgObj.css({
			height:winHeight - autoTune,
			width:winHeight*imgRate
			//marginLeft:(winWidth - winHeight*imgRate)/2,
			//marginTop:0
		});
		return false;
	}
}

//获得窗口高度
function getTotalHeight(){ 
	if($.browser.msie){ 
		return document.compatMode == "CSS1Compat"? document.documentElement.clientHeight : document.body.clientHeight; 
	} 
	else { 
		return self.innerHeight; 
	} 
} 
//获得窗口宽度
function getTotalWidth (){ 
	if($.browser.msie){ 
		return document.compatMode == "CSS1Compat"? document.documentElement.clientWidth : document.body.clientWidth; 
	} 
	else{ 
		return self.innerWidth; 
	} 
} 
//获取图片的真实高度

function GetImageWidth(oImage)
{
	var OriginImage=new Image();
	if(OriginImage.src!=oImage.src)OriginImage.src=oImage.src;
	return OriginImage.width;
}
function GetImageHeight(oImage)
{
	var OriginImage=new Image();
	if(OriginImage.src!=oImage.src)OriginImage.src=oImage.src;
	return OriginImage.height;
}

//IE下 全屏
function goFullWindow(dom){
	$(dom).live("click",function(){
        window.opener = null;
        var screenWidth = screen.availWidth, screenHeight = screen.availHeight;
        var args = "toolbar=no, menubar=no, scrollbars=yes,resizable=yes,location=no, status=no";       
        //打开全屏的新窗口
        var win = window.open(window.location.href,"_blank",args);
        if(win){
            win.resizeTo(screenWidth,screenHeight);
            win.outerWidth = screenWidth;
            win.outerHeight = screenHeight;
            win.moveTo(0,0);
        }
        window.open('','_self');
        window.close();
	})

}
//重写SETTIMEOUT
var __sto = setTimeout;
window.setTimeout = function(callback,timeout,param){
	var args = Array.prototype.slice.call(arguments,2);
	var _cb = function(){
		callback.apply(null,args);
	}
	__sto(_cb,timeout);
} 


